package com.shop.shopserver.controller.user;

import com.shop.shopcommon.result.Result;
import com.shop.shoppojo.dto.GoodsSearchDTO;
import com.shop.shoppojo.vo.GoodsSearchVO;
import com.shop.shopserver.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

import static com.shop.shopcommon.constant.MessageConstant.SUCCESS;

@RestController
@Api(tags = "商品相关搜索模块")
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;
    @PostMapping("/getSearchList")
    @ApiOperation(value = "搜索框商品搜索")
    public Result<GoodsSearchVO> getSearchList (@RequestBody GoodsSearchDTO goodsSearchDTO) throws IOException {
        if (goodsSearchDTO.getGoodsName()!=null){
            // 搜索框搜索进入商品列表
            GoodsSearchVO goodsSearchVO = searchService.getSearchList(goodsSearchDTO);
            return Result.success(goodsSearchVO,SUCCESS);
        }
        // 分类也进入商品列表
        GoodsSearchVO goodsSearchVO = searchService.getSearchListByCategoryId(goodsSearchDTO);
        return Result.success(goodsSearchVO,SUCCESS);
    }
}
